﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using OgilvyOne.Model;
using OgilvyOne.DALFactory;
using OgilvyOne.IDAL;
using OgilvyOne.Utility;



namespace OgilvyOne.BLL
{
    /// <summary>
    /// TemplateLabelBll
    /// </summary>
    public partial class TemplateLabelBll
    {
        private readonly ITemplateLabel dal =TemplateLabelFactory.create();
        public TemplateLabelBll()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int LABEL_ID)
        {
            return dal.Exists(LABEL_ID);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name,int id)
        {
            return dal.Exists(name,id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(TemplateLabelModel model)
        {
            
            bool re = dal.Add(model);
            if(re) SaveHtml(model);
            return re;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TemplateLabelModel model)
        {
            bool re = dal.Update(model);
            if(re) SaveHtml(model);
            return re;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int LABEL_ID)
        {

            return dal.Delete(LABEL_ID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string LABEL_IDlist)
        {
            return dal.DeleteList(LABEL_IDlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TemplateLabelModel GetModel(int LABEL_ID)
        {

            return dal.GetModel(LABEL_ID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TemplateLabelModel> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TemplateLabelModel> DataTableToList(DataTable dt)
        {
            List<TemplateLabelModel> modelList = new List<TemplateLabelModel>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                TemplateLabelModel model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new TemplateLabelModel();
                    if (dt.Rows[n]["LABEL_ID"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["LABEL_ID"].ToString());
                    }
                    model.Name= dt.Rows[n]["LABEL_NAME"].ToString();
                    model.Def = dt.Rows[n]["LABEL_DEF"].ToString();
                    model.Value= dt.Rows[n]["LABEL_VALUE"].ToString();
                    if (dt.Rows[n]["ADDTIME"].ToString() != "")
                    {
                        model.Addtime = DateTime.Parse(dt.Rows[n]["ADDTIME"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        private Hashtable labelmap;
        /// <summary>
        /// 获取LABEL MAP
        /// </summary>
        /// <returns></returns>
        public Hashtable GetLabelMap()
        {
            if (labelmap == null || CofcoDynamicSet.isLabelUpdate == true)
            {
                labelmap = dal.GetLabelHashtable();
            }

            return labelmap;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        private void SaveHtml(TemplateLabelModel model)
        {
            if (model.Ishtml == CmsConstants.YES || (model.HtmlFileName!=null && model.HtmlFileName.IndexOf(".html")>-1))
            {
                //CommonModel cmsmodel = CmsProxy.GetModel(model.ModelType, null);
                CommonModel cmsmodel = new ArticleTemplateType(null);
                ICmsLabel cl = LabelProxy.GetCmsLabelLabel(cmsmodel, model.Def); 
                string filename = System.Web.HttpContext.Current.Server.MapPath("~/include/tag/") + model.HtmlFileName;
                FileUtil.SaveHtmlFileNoBom(filename, cl.GetLabelHtml());
            }
        }
        /// <summary>
        /// 栏目内容变更生成对应模板标签HTML
        /// </summary>
        /// <param name="labels"></param>
        public void ColumnSaveLabelsHtml(string labels)
        {
            try
            {
                if (labels != null && labels != "")
                {
                    if (labels.Substring(labels.Length - 1) == ",") labels = labels.Remove(labels.Length - 1);
                    DataSet ds = dal.GetList(" LABEL_ID in (" + labels + ")");
                    DataTable dt = ds.Tables[0];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TemplateLabelModel model = new TemplateLabelModel();
                        int lid = int.Parse(dt.Rows[i]["LABEL_ID"].ToString());
                        model.Id = lid;
                        try
                        {
                            model.Ishtml = int.Parse(dt.Rows[i]["IS_HTML"].ToString());
                        }
                        catch { }
                        model.HtmlFileName = dt.Rows[i]["HTML_FILE_NAME"].ToString();
                        model.Def = dt.Rows[i]["LABEL_DEF"].ToString();
                        model.Value = dt.Rows[i]["LABEL_VALUE"].ToString();
                        try
                        {
                            model.ModelType = int.Parse(dt.Rows[i]["MODEL_TYPE"].ToString());
                        }
                        catch { }
                        model.LabelType = dt.Rows[i]["LABEL_TYPE"].ToString();



                        SaveHtml(model);
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
           
        }
        /// <summary>
        /// 所有HTML标签生成
        /// </summary>
        public void CreateAllLabelHtml()
        {
            try
            {
            DataSet ds = dal.GetList(" IS_HTML = 1");
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TemplateLabelModel model = new TemplateLabelModel();
                int lid = int.Parse(dt.Rows[i]["LABEL_ID"].ToString());
                model.Id = lid;
                try
                {
                    model.Ishtml = int.Parse(dt.Rows[i]["IS_HTML"].ToString());
                    if (model.Ishtml == CmsConstants.YES) model.HtmlFileName = dt.Rows[i]["HTML_FILE_NAME"].ToString();
                }
                catch
                {
                }

                model.Def = dt.Rows[i]["LABEL_DEF"].ToString();
                model.Value = dt.Rows[i]["LABEL_VALUE"].ToString();
                try
                {
                    model.ModelType = int.Parse(dt.Rows[i]["MODEL_TYPE"].ToString());
                }
                catch
                {
                }
                if (dt.Rows[i]["LABEL_TYPE"] != null) model.LabelType = dt.Rows[i]["LABEL_TYPE"].ToString();

                SaveHtml(model);
            }
            }
            catch (Exception)
            {
                
                throw;
            }
           
           
        }

        #endregion  Method
    }
}

